<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace plugins\qcoludsms;

use cmf\lib\Plugin;
use plugins\qcoludsms\lib\SmsSingleSender;
use think\Exception;

/**
 * QcoludsmsPlugin
 */
class QcoludsmsPlugin extends Plugin
{

    public $info = [
        'name' => 'Qcoludsms',
        'title' => '腾讯云短信接口',
        'description' => '腾讯云手机验证码',
        'status' => 1,
        'author' => 'yy',
        'version' => '1.0'
    ];

    public $has_admin = 0;//插件是否有后台管理界面

    public function install() //安装方法必须实现
    {
        return true;//安装成功返回true，失败false
    }

    public function uninstall() //卸载方法必须实现
    {
        return true;//卸载成功返回true，失败false
    }

    //实现的send_mobile_verification_code钩子方法
    public function sendMobileVerificationCode($param)
    {
        try {
            // 请根据实际 appid 和 appkey 进行开发，以下只作为演示 sdk 使用
            $config = $this->getConfig();
            $appid = $config['app_id'];
            $appkey = $config['app_key'];
            $templId = $config['template_id'];
            $sign = '';
            if(!empty($config['sign'])) {
                $sign = $config['sign'];
            }
            $expire_minute = intval($config['expire_minute']);
            $mobile = $param['mobile'];//手机号
            $code = $param['code'];//验证码

            $singleSender = new SmsSingleSender($appid, $appkey);
            $result = $singleSender->sendWithParam("86", $mobile, $templId, [
                $code, $expire_minute
            ], $sign, "");

            $rsp = json_decode($result, true);
            if ($rsp && $rsp['result'] == 0) {
                return true;
            }
            return [
                'error' => 1,
                'message' => $rsp['errmsg']
            ];
        } catch (Exception $e) {
            return false;
        }
    }

}